/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GUIPrincipal.java
 *
 * Created on 15/05/2011, 10:32:25
 */

package gui;

import connection.Conexao;
import dao.DAOCliente;
import dao.ResultSetComboBox;
import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;
import javax.swing.*;
import javax.swing.JOptionPane;
import model.KeyValue;
import model.CryptoUtils;

/**
 *
 * @author Proprietário
 */
public class GUIPrincipal extends javax.swing.JFrame {

    private Properties hostInfo = new Properties();
    final String CHAVE_PUBLICA = "sysdesk_public.key";



    /** Creates new form GUIPrincipal */
    public GUIPrincipal() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jlComboValue = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jbConexaoBD = new javax.swing.JButton();
        jcbOperador = new javax.swing.JComboBox();
        jPanel2 = new javax.swing.JPanel();
        jlComboValue2 = new javax.swing.JLabel();
        jcbListaClientes = new javax.swing.JComboBox();
        jbGridCliente = new javax.swing.JButton();
        jbComboValue = new javax.swing.JButton();
        jbClienteCombo = new javax.swing.JButton();
        jbLoadCliente = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jtfCriptografaString = new javax.swing.JTextField();
        jtfCriptografado = new javax.swing.JTextField();
        jbCriptografa = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("SysDesk - Gerenciador de chamados");
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Banco de dados", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION));

        jLabel1.setText("...");

        jbConexaoBD.setFocusable(false);
        jbConexaoBD.setMargin(new java.awt.Insets(2, 2, 2, 2));
        jbConexaoBD.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbConexaoBDActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 498, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbConexaoBD, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel1)
                    .addComponent(jbConexaoBD, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jcbOperador.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Operador" }));

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Selecionar Clientes", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION));

        jlComboValue2.setText("Código");

        jcbListaClientes.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "           ▼ Clique em carregar ▼" }));
        jcbListaClientes.setAutoscrolls(true);
        jcbListaClientes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbListaClientesActionPerformed(evt);
            }
        });

        jbGridCliente.setText("Mostrar tabela de clientes");
        jbGridCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbGridClienteActionPerformed(evt);
            }
        });

        jbComboValue.setText("Exibe código do cliente");
        jbComboValue.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbComboValueActionPerformed(evt);
            }
        });

        jbClienteCombo.setText("▲ Carregar lista de empresas ▲");
        jbClienteCombo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbClienteComboActionPerformed(evt);
            }
        });

        jbLoadCliente.setText("Carregar cadastro do cliente");
        jbLoadCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbLoadClienteActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jbGridCliente, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jbLoadCliente, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jbClienteCombo, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jcbListaClientes, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup()
                        .addComponent(jbComboValue)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jlComboValue2)))
                .addContainerGap(44, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jcbListaClientes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jbClienteCombo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbComboValue)
                    .addComponent(jlComboValue2))
                .addGap(7, 7, 7)
                .addComponent(jbLoadCliente)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbGridCliente)
                .addContainerGap(62, Short.MAX_VALUE))
        );

        jLabel4.setText("Escolha o seu cadastro ao lado e depois selecione o que você quer fazer abaixo:");

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Criptografia", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION));

        jtfCriptografaString.setText("Informe a String a ser criptografada");

        jtfCriptografado.setText("Valor Criptografado");

        jbCriptografa.setText("Criptografa!");
        jbCriptografa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbCriptografaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jtfCriptografaString, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 226, Short.MAX_VALUE)
                    .addComponent(jtfCriptografado, javax.swing.GroupLayout.DEFAULT_SIZE, 226, Short.MAX_VALUE)
                    .addComponent(jbCriptografa, javax.swing.GroupLayout.DEFAULT_SIZE, 226, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(jtfCriptografaString, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jbCriptografa)
                .addGap(18, 18, 18)
                .addComponent(jtfCriptografado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(167, Short.MAX_VALUE))
        );

        jPanel4.setBackground(new java.awt.Color(102, 0, 102));

        jLabel2.setFont(new java.awt.Font("Pirulen Rg", 1, 14));
        jLabel2.setForeground(new java.awt.Color(255, 255, 255));
        jLabel2.setText("<SYSDESK>");

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGap(16, 16, 16)
                .addComponent(jLabel3)
                .addGap(18, 18, 18)
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(407, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addGap(41, 41, 41)
                            .addComponent(jLabel4)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jcbOperador, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jlComboValue))
            .addComponent(jPanel4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(213, 213, 213)
                        .addComponent(jlComboValue)
                        .addGap(190, 190, 190))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(jcbOperador, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbGridClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbGridClienteActionPerformed
        
        GUIClienteTeste clienteTeste = new GUIClienteTeste();
        clienteTeste.setVisible(true);
    }//GEN-LAST:event_jbGridClienteActionPerformed

    private void conectarAoBanco(){
        // se o banco ja estiver conectado, entao desconecta
        if (Conexao.isConnected()) {

            Conexao.desconectar();
            jLabel1.setText("Banco desconectado");
            jbConexaoBD.setIcon(new ImageIcon(ClassLoader.getSystemResource("images/bullet_red.png")));

        } else {

            try{
                //carrega dados do banco
                hostInfo.load(new FileInputStream("sysdesk.ini"));
                
                // faz a conexao
                Conexao.setDbBanco(hostInfo.getProperty("banco"));
                Conexao.setDbDriver(hostInfo.getProperty("driver"));
                Conexao.setDbUsuario(CryptoUtils.decrypt(hostInfo.getProperty("usuario"), new File(CHAVE_PUBLICA)));
                Conexao.setDbSenha(CryptoUtils.decrypt(hostInfo.getProperty("senha"), new File(CHAVE_PUBLICA)));

                if (Conexao.conectar() == true){
                  jLabel1.setText("Banco conectado");
                  jbConexaoBD.setIcon(new ImageIcon(ClassLoader.getSystemResource("images/bullet_green.png")));
                } else {
                  jLabel1.setText("Banco não conectado");
                  jbConexaoBD.setIcon(new ImageIcon(ClassLoader.getSystemResource("images/bullet_red.png")));
                }

            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, "erro na conexão com o banco:"+e.getMessage());
            } // fim do try catch
        }

    }

    private void jbConexaoBDActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbConexaoBDActionPerformed
        conectarAoBanco();
    }//GEN-LAST:event_jbConexaoBDActionPerformed

    private void jbClienteComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbClienteComboActionPerformed
        DAOCliente clientes = new DAOCliente();

        try{
            jcbListaClientes.setModel(new ResultSetComboBox(clientes.getListaClientes(), "cli_nome", "cli_codigo").getModel() );
        } catch (Exception e) {
            System.out.println("erro: "+e.getMessage());
        }
    }//GEN-LAST:event_jbClienteComboActionPerformed

    private void jbComboValueActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbComboValueActionPerformed
        jlComboValue2.setText(((KeyValue) jcbListaClientes.getSelectedItem()).getValue());
    }//GEN-LAST:event_jbComboValueActionPerformed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        if (Conexao.isConnected() == false) {
         conectarAoBanco();
        }
    }//GEN-LAST:event_formWindowOpened

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        if (Conexao.isConnected() == true) {
            conectarAoBanco();
        }
    }//GEN-LAST:event_formWindowClosing

    private void jbCriptografaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbCriptografaActionPerformed
        try{
            jtfCriptografado.setText(CryptoUtils.encrypt(jtfCriptografaString.getText(), new File(CHAVE_PUBLICA)));
        } catch (Exception e) {
            System.out.println("Erro ao criptografar.");
        }
    }//GEN-LAST:event_jbCriptografaActionPerformed

    private void jbLoadClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbLoadClienteActionPerformed
        
        GUICadastroClientes clienteCadastro = new GUICadastroClientes();
        clienteCadastro.setVisible(true);
    }//GEN-LAST:event_jbLoadClienteActionPerformed

    private void jcbListaClientesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcbListaClientesActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jcbListaClientesActionPerformed


    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GUIPrincipal().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JButton jbClienteCombo;
    private javax.swing.JButton jbComboValue;
    private javax.swing.JButton jbConexaoBD;
    private javax.swing.JButton jbCriptografa;
    private javax.swing.JButton jbGridCliente;
    private javax.swing.JButton jbLoadCliente;
    private javax.swing.JComboBox jcbListaClientes;
    private javax.swing.JComboBox jcbOperador;
    private javax.swing.JLabel jlComboValue;
    private javax.swing.JLabel jlComboValue2;
    private javax.swing.JTextField jtfCriptografaString;
    private javax.swing.JTextField jtfCriptografado;
    // End of variables declaration//GEN-END:variables

}
